package com.mango.iot.gateway.coap.server.resource;

import com.mango.iot.gateway.coap.common.CoapResourceConst;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.californium.core.coap.CoAP;
import org.eclipse.californium.core.coap.CoAP.Type;
import org.eclipse.californium.core.server.resources.CoapExchange;
import org.springframework.stereotype.Component;

/**
 * 设备属性配置API
 * 开启订阅监听模式
 *
 * @author liangfeihu
 * @number 53669
 * @since 2021/4/27 下午4:04
 */
@Slf4j
@Component
public class ConfigResource extends BaseCoapResource {

    public ConfigResource() {
        super(CoapResourceConst.CONFIG);

        // enable observing
        setObservable(true);
        // configure the notification type to CONs
        setObserveType(Type.CON);

        getAttributes().setTitle("Config Resource");
        // mark observable in the Link-Format
        getAttributes().setObservable();

    }

    @Override
    public void handleGET(CoapExchange exchange) {
        String token = getToken(exchange);
        log.info("[ConfigResource] handleGET token={}", token);
        if (StringUtils.isBlank(token)) {
            // request doesn't contain token：未登录，没权限
            exchange.respond(CoAP.ResponseCode.UNAUTHORIZED);
        }
        // todo: 处理变更属性给设备

    }

}
